package com.itheima.reggie_auto.controller;


import com.itheima.reggie_auto.common.R;
import com.itheima.reggie_auto.entity.User;
import com.itheima.reggie_auto.service.UserService;
import com.itheima.reggie_auto.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author 刘少宽
 * @since 2022-09-23
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        //发送验证码
        // SMSUtils.sendMessage();

        //设置到session域中
        // session.setAttribute(phone,"1234");

        //将验证码设置到缓存中  ，存活时长5分钟
        redisTemplate.opsForValue().set(phone,"1234",5, TimeUnit.MINUTES);

        log.info("验证码为：{}",1234);
        return R.success("验证码发送成功");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpSession session) {

        String phone = map.get("phone").toString();
        String code = map.get("code").toString();



        //从session中获取验证码
        // String checkCode = (String) session.getAttribute(phone);

        //从redis中获取验证码
        String checkCode = (String) redisTemplate.opsForValue().get(phone);

        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return R.error("手机号和验证码不能为空");
        }
        if (checkCode == null) {
            return R.error("请获取验证码");
        }
        if (code.equals(checkCode)) {
            //登录成功

            //判断数据库中是否存在该用户  不存在则新建
            User user = userService.getByPhone(phone);
            if (user == null) {
                //添加用户
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            // session.removeAttribute("employee");
            session.setAttribute("user", user.getId());

            // 如果登录成功则删除redis缓存的验证码
            redisTemplate.delete(phone);

            return R.success("登陆成功");
        }


        return R.error("验证码错误");
    }
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        //分析：删除session即可
        session.invalidate();
        return R.success("退出登录成功");
    }


}

